﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Service.Comments
{

    public class NicoCommentBody
    {

        private INicoCommentBodyText[] texts;

        public ICollection<INicoCommentBodyText> Texts
        {
            get
            {
                return texts;
            }
        }

        public override string ToString()
        {
            string body = "";
            
            ToString((s) =>
            {
                body += s.Text;
            });

            return body;
        }

        public bool IsCommand
        {
            get
            {
                return this.Texts.First().IsCommand;
            }
        }

        
        public bool IsUserShowCommand
        {
            get
            {
                return this.Texts.First().IsUserShowCommand;
            }
        }

        public bool IsNoneShowCommand
        {
            get
            {
                return this.Texts.First().BodyType == NicoCommentBodyTextType.NoneShowCommand;
            }
        }

        public bool IsOwnerShowCommand
        {
            get
            {
                return this.Texts.First().IsOwnerShowCommand;
            }
        }
        
        private void toString(IEnumerable<INicoCommentBodyText> texts, Action<INicoCommentBodyText> action)
        {
            foreach (var text in texts)
            {
                if (text.HasChildBodyTexts) toString(text.ChildTexts, action);
                else if (text.HasCurrentBodyText) action(text);
                
            }
        }

        private void toDisplayString(IEnumerable<INicoCommentBodyText> texts, Action<INicoCommentBodyText> action)
        {
            foreach (var text in texts)
            {
                
                if (text.HasCurrentBodyText) action(text);

            }
        }

        private readonly static NicoCommentBodyParser parser = new NicoCommentBodyParser();

        public static NicoCommentBody Parse(NotifyNicoComment comment,string body)
        {
            lock (parser)
            {
                NicoCommentBody commentBody = new NicoCommentBody();
                commentBody.texts = parser.Parse(comment,body);

                return commentBody;

            }

        }


        public void ToDisplayString( Action<INicoCommentBodyText> action)
        {
            toDisplayString(Texts, action);
        }


        public void ToString(Action<INicoCommentBodyText> action)
        {
            toString(Texts, (s) =>
            {
                action(s);
            });
        }

        public string ToDisplayString()
        {
            string body = "";

            ToDisplayString((s) =>
            {
                body += s.DisplayText;
            });

            return body;
        }
    }
}
